<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
    <link rel="stylesheet" type="text/css" href="/static/taginput/css/normalize.css" /><!--CSS RESET-->
    <link rel="stylesheet" href="/static/taginput/src/jquery.tagsinput-revisited.css" />
    <style>
        .layui-form-select .layui-input{
            width:400px;
        }
    </style>
</head>
<body>
<div style="padding-top:10px;" class="layui-form layuimini-form">
    <input type="hidden" name="id" value="{$res.id}">



    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">类型</label>
        <div class="layui-input-block">
            <input type="text"     disabled value="{$res.type}" class="layui-input iwidth" autocomplete="off">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">banner上传</label>
        <input type="hidden" id="banner_images" name="banner" value="{$res.banner}">
        <div class="layui-upload">
            <div class="layui-form-mid layui-word-aux">
                <button type="button" class="layui-btn" id="multiple_banner">多图片上传</button>
                <span class="x-red">*</span>为保证图片质量，轮播图片请尽量选择200*200的方形图片,图片大小不要超过500KB,一般在30KB-100KB之间
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    预览：
                    <div class="layui-upload-list" id="div-slide_show">
                        {volist name="$bannerArr" id='ba'}
                        <img src="/upload/{$ba}" alt="{$ba}" title="点击删除" style="width: 100px;height: 100px" class="layui-upload-img" onclick="delMultipleImgs(this,'banner')">
                        {/volist}
                    </div>
                </blockquote>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script src="/static/taginput/src/jquery.tagsinput-revisited.js"></script>


<script>
    let banner_url=[{$bannerurl}];


    layui.use(['form','upload'], function () {
        let form = layui.form,
            upload=layui.upload,
            $ = layui.$;

        //轮播图多图片上传
        upload.render({
            elem: '#multiple_banner',
            url: '/admin/banner/Doupload',
            data: {
                'mark': 'img',
                'field': 'activeBanner',
                'oldSrc': ''
            },
            field: 'activeBanner',
            accept: 'images',
            acceptMime: 'image/*',
            multiple: true,
            before: function(obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result) {
                    $('#div-slide_show').append('<img src="' + result + '" alt="' + file.name +
                        '" title="点击删除" style="width: 100px;height: 100px" class="layui-upload-img" onclick="delMultipleImgs(this,\'banner\')">')
                });
            },
            done: function(res) {
                //如果上传成功
                if (res.status == true) {
                    //追加图片成功追加文件名至图片容器
                    banner_url.push(res.src);
                    $('#banner_images').val(banner_url);
                }
            },
            allDone: function(obj) {

            }
        });


        //监听提交
        form.on('submit(saveBtn)', function (data) {

            console.log(data.field)
            $.ajax({
                url: "/admin/banner/save?active=ajax",
                type: 'post',
                async: true,
                data: data.field,
                dataType: 'json',
                success: function (res) {
                    if(res.status){
                        parent.layer.msg("操作成功!", {icon:1,time: 1000}, function () {
                            //重新加载父页面
                            parent.location.reload();
                        });
                        return;
                    }else{
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });
    });



    /**
     *
     * @context 删除图片
     * @param this_img
     * @param type
     */
    function delMultipleImgs(this_img, type) {

        //获取下标
        var subscript = $("#div-slide_show img").index(this_img);
        //console.log(banner_url)
       // return false
        //删除图片
        this_img.remove();
        if (type == 'banner') {
            //删除轮播图数组
            banner_url.splice(subscript, 1);

            $('#banner_images').val(banner_url);
        } else {
            //删除详情图数组
            picurl_images.splice(subscript, 1);
            $('#picurl_images').val(picurl_images);
        }
        //返回
        return true;
    }
</script>
</body>
</html>